package com.dissertation.action;

import com.dissertation.pojo.Message;
import com.dissertation.service.IDownload;
import com.dissertation.service.IFileupload;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

@Controller
public class FileController {

    @Resource( name = "fileUploadGridSF" )
    IFileupload fileupload;


//    @RequiresPermissions( "insert" )
    @PostMapping( "files" )
    @ResponseBody
    Message fileupload( @RequestParam( "files" ) MultipartFile[] files ) throws IOException {
        if ( files == null ) {
            return Message.createErr( "对不起，不能上传空文件" );
        }

        return Message.createSuc( fileupload.fileUpload( Arrays.asList( files ) ) );
    }



    @Resource( name = "gridSFDownload" )
    IDownload download;

    @GetMapping( "file/{id}" )
    void download( @PathVariable String id,
                   HttpServletRequest request,
                   HttpServletResponse response ) throws IOException {
        download.download(id, response, request);
    }



}
